const vm = require('vm');
const fs = require('fs');

class Resource{
    constructor(){ }
    load(){
        this.client_sock.state=2;
        let body = [];
        if( !fs.existsSync(`./srcipt${this.header.path}`) ){
            body.push(`<p>not found ${this.header.path}</p>`);
            return this.Emit(body, 404);
        }

        let code=fs.readFileSync(`./srcipt${this.header.path}`,'utf-8');
        const script = new vm.Script(code);
        /* 相关参数 */
        const context = {
            require:require,
            root_path:`${__dirname}/srcipt/`,
            SESSION:null,
            HEADER: JSON.parse( JSON.stringify(this.header) ),
            echo:function(str){
                body.push(str);
            }
        };
        let ctx=vm.createContext(context);
        script.runInContext(ctx);
        ctx.main();
        return this.Emit(body);
    }
}
module.exports=Resource;